<template>
    <div style="width: 100%;">
        <!-- 搜索表单 -->
        <el-form :inline="true" ref="formRef" :model="formInline" class="demo-form-inline">
            <el-form-item label="工号">
                <el-input v-model="formInline.staffNo" placeholder="请输入工号" clearable />
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="formInline.staffName" placeholder="请输入姓名" clearable />
            </el-form-item>
            <el-form-item label="打卡类型">
                <el-select v-model="formInline.type" placeholder="请选择打卡类型" clearable>
                    <el-option v-for="department in typeList" :key="department.value" :label="department.label"
                        :value="department.label"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="search">
                <el-button type="primary" :icon="Refresh" @click="resetFormInline">重置</el-button>
                <el-button type="primary" :icon="Search" @click="searchData">查询</el-button>
            </el-form-item>
        </el-form>
        <!-- 按钮组 -->
        <div class="btn-group">
            <el-button type="danger" @click="handleDelete">删除</el-button>
        </div>
        <!-- 表格 -->
        <div class="table">
            <el-table :data="tableData" style="width: 100%" border height="420" show-overflow-tooltip
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="40" label="全选" />
                <el-table-column fixed label="索引" type="index" :index="indexMethod" align="center" width="55" />
                <el-table-column prop="staff_no" label="工号" />
                <el-table-column prop="staff_name" label="姓名" />
                <el-table-column prop="position" label="职位" />
                <el-table-column prop="type" label="打卡类型" />
                <el-table-column prop="time" label="打卡时间" :formatter="formatTime" />
                <el-table-column fixed="right" label="操作" width="180">
                    <template #default="scope">
                        <el-button type="primary" @click="handleDetail(scope.row)">详情</el-button>
                        <el-button type="danger" @click="deleteRows(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination-wrapper">
            <el-pagination class="pagination" v-model:current-page="page.pageNum" v-model:page-size="page.pageSize"
                :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper" :total="page.total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
        <!-- 弹窗-员工详情 -->
        <el-dialog class="dialog" v-model="dialogVisible" title="员工详情" width="800" :close="handleClose">
            <el-form class="dialog-form" :inline="true" :model="dialogForm" ref="dialogFormRef">
                <el-form-item label-width="120px" label="工号">
                    <el-input v-model="dialogForm.staffNo" disabled clearable />
                </el-form-item>
                <el-form-item label-width="120px" label="姓名">
                    <el-input v-model="dialogForm.name" disabled clearable />
                </el-form-item>
                <el-form-item label-width="120px" label="职位">
                    <el-input v-model="dialogForm.position" disabled clearable />
                </el-form-item>
                <el-form-item label-width="120px" label="部门">
                    <el-input v-model="dialogForm.departmentName" disabled clearable />
                </el-form-item>
                <el-form-item label-width="120px" label="打卡类型">
                    <el-input v-model="dialogForm.type" disabled clearable />
                </el-form-item>
                <el-form-item label-width="120px" label="打卡时间">
                    <el-input v-model="dialogForm.time" disabled clearable />
                </el-form-item>
                <el-form-item label-width="120px" label="备注">
                    <el-input style="width: 220px;" v-model="dialogForm.remarks" type="textarea" clearable />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="handleClose">关闭</el-button>
                    <el-button type="primary" @click="updateDialog">
                        修改
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'
import { queryAttendance, delAttendance, updateAttendance } from "@/api/employee"
import { timeProcessing } from '@/utils/date'

// 分页
const page = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
})

// 表单-搜索-ref
const formRef = ref(null)

// 表单-搜索-数据
const formInline = reactive({
    staffNo: '',
    staffName: '',
    type: '',
})

const typeList = reactive([
    { value: '签到', label: '签到' },
    { value: '签退', label: '签退' }
])

// 表格数据
const tableData = ref([])

// 详情-弹窗
const dialogVisible = ref(false)
const dialogFormRef = ref(null)
const dialogForm = reactive({})

// 多选-id
const ids = ref([]);

// 表格索引
const indexMethod = (index) => {
    return page.pageSize * (page.pageNum - 1) + index + 1
}

const formatTime = (row, column, cellValue) => {
    return timeProcessing(cellValue)
}

// 重置
const resetFormInline = () => {
    formInline.staffNo = '';
    formInline.staffName = '';
    formInline.type = '';
    searchData();
}

// 搜索
const searchData = async () => {
    const data = {
        staffNo: formInline.staffNo,
        staffName: formInline.staffName,
        type: formInline.type,
        pageNum: page.pageNum,
        pageSize: page.pageSize,
    }
    try {
        const res = await queryAttendance(data);
        if (res.code === 0) {
            tableData.value = res.data.list;
            page.total = res.data.total;
        } else {
            // ElMessage.error(res.msg);
        }
    } catch {
        console.log("请求失败");
    }
}

// 表格多选
const handleSelectionChange = (val) => {
    ids.value = val.map(item => item.id);
}

// 删除
const deleteRows = (ids) => {
    const data = Array.isArray(ids) ? ids : [ids];
    ElMessageBox.confirm('确认删除选中项吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        try {
            const res = await delAttendance(data);
            if (res.code === 0) {
                ElMessage.success('删除成功！');
                searchData();
            } else {
                ElMessage.error('删除失败！');
            }
        } catch {
            console.log("请求失败");
        }
    }).catch(() => {
        console.log('取消删除');
    })
}

// 多选删除
const handleDelete = () => {
    if (ids.value.length === 0) {
        ElMessage.warning('请选择要删除的签到记录！');
        return
    }
    deleteRows(ids.value)
}

// 详情-弹窗-修改
const updateDialog = async () => {
    console.log('dialogForm: ', dialogForm);
    const data = {
        id: dialogForm.id,
        remarks: dialogForm.remarks,
    }
    try {
        const res = await updateAttendance(data);
        if (res.code == 0) {
            ElMessage.success('修改成功！');
            handleClose();
            searchData();
        } else {
            ElMessage.error('修改失败！');
        }
    } catch {
        console.log("请求失败");
    }
}

// 详情-弹窗-打开
const handleDetail = (row) => {
    dialogVisible.value = true;
    dialogForm.id = row.id;
    dialogForm.staffNo = row.staff_no;
    dialogForm.name = row.staff_name;
    dialogForm.position = row.position;
    dialogForm.departmentName = row.department_name;
    dialogForm.type = row.type;
    dialogForm.time = timeProcessing(row.time);
    dialogForm.remarks = row.remarks;
}

// 详情-弹窗-关闭
const handleClose = () => {
    dialogVisible.value = false
    if (dialogFormRef.value) dialogFormRef.value.resetFields();
}

// 分页
const handleSizeChange = (val) => {
    page.pageSize = val
    searchData()
}

const handleCurrentChange = (val) => {
    page.pageNum = val
    searchData()
}

onMounted(() => {
    searchData();
})


</script>

<style lang="scss" scoped>
.demo-form-inline {
    padding: 10px 20px 0;
}

.demo-form-inline .el-input {
    --el-input-width: 150px;
}

.demo-form-inline .el-select {
    --el-select-width: 150px;
}

.btn-group {
    box-sizing: border-box;
    padding-right: 20px;
    margin-bottom: 5px;
    margin-top: -15px;
    float: right;
}

.table {
    box-sizing: border-box;
    padding: 0 20px 0 15px;
}

.footer {
    position: relative;
    width: 100%;
    height: auto;
}

.pagination-wrapper {
    width: 100%;
    height: 44px;
    display: flex;
    justify-content: flex-end;
}

.pagination {
    margin-top: 15px;
    margin-right: 20px;
}

:deep(.el-dialog__body) {
    display: flex;
    justify-content: center;
}

.dialog-form {
    .el-input {
        --el-input-width: 220px;
    }
}
</style>